from typing import *


class Solution:

    def findMaxLength(self, nums: List[int]) -> int:
        h = {0: -1}
        ans = o = z = 0
        for i, v in enumerate(nums):
            o += v
            z += v ^ 1
            diff = o - z
            if diff in h:
                ans = max(ans, i - h[diff])
            else:
                h[diff] = i
        return ans
